﻿import com.dinmax.flash.gsa.sgs.*
import com.dinmax.flash.gsa.sgs.bridge.*
import com.dinmax.flash.gsa.sgs.bridge.slot.*
import com.dinmax.flash.gsa.sgs.game.*
import com.dinmax.flash.gsa.sgs.player.*

class SGSBridge {
	
	private var GAMES_LEVEL:Number=10;
	private var GAMES_LEVEL_MAX:Number=20;
	private var GAME_ACTIVE_LEVEL:Number=21;
	private var _games:SGSGames;
	private var parent:MovieClip;
	private var loader:MovieClipLoader;
	private var loaderList:Object;

	private static var bridge:SGSBridge;

	public static function newInstance(parent:MovieClip):Void {
		bridge= new SGSBridge(parent);
	}; //fin función
	
	
	public static function getActive():SGSBridge {
		return bridge;
	}; //fin función
	
	function SGSBridge(parent:MovieClip){
		this.parent=parent;
		
		loaderList=new Object();
			
			loaderList.onLoadError=function(){
				trace("onLoadError - SGSBridge");
			};
			
			loaderList.onLoadComplete=function(){

			};

		loader=new MovieClipLoader();
		loader.addListener(loaderList);
		init(this);
	};
	
	
	public function registerMachine(serial:String):Void {
		SGS.newInstance(this, serial);
	}; //fin función
	

	
	////FUNCIONES INTERNAS

	

	////FUNCIONES DEL SGS AL SWF
	
	public function SGS_2_SWF_disableMachine(msg:String):Void {
		loaderList.onLoadInit=function (target:MovieClip){
			target.mcCartel.txt_field.text="Error: "+msg;
		};
		for(var i:Number=GAMES_LEVEL; i < GAME_ACTIVE_LEVEL+1; i++){
			loader.unloadClip(i);
		};
		loader.addListener(loaderList);
		if(_level100 == undefined){
			loader.loadClip("cartel.swf", 100);
			loader.loadClip("disabled.swf", 99);
			parent.btn_led.gotoAndStop("apagado");
		} else {
			_level100.mcCartel.txt_field.text+="\nError: "+msg;
		};
		//loader.unloadClip(GAME_ACTIVE_LEVEL);
		parent.mcPlayer.gotoAndStop("logIn");
		init(this);
				
	}; //fin función

	public function SGS_2_SWF_enableMachine():Void {
		loader.unloadClip(100);
		loader.unloadClip(99);
		parent.btn_led.gotoAndStop("ok");
				
	}; //fin función

	public function SGS_2_SWF_inCommunication():Void {
		loader.loadClip("disabled.swf", 99);
		parent.btn_led.gotoAndPlay("tilt");
	}; //fin función

	public function SGS_2_SWF_lostCommunication():Void {
		parent.btn_led.gotoAndStop("error");
	}; //fin función
	
	
	public function SGS_2_SWF_loadGames(games:SGSGames):Void {
		_games=new SGSGames();
		trace("SGSBridge.as | loadGames()= "+games);
		loaderList.onLoadInit=function (target:MovieClip){
			target._x=(_root._width - target._width)/2;
			target._y=(_root._height - target._height)/2;
			target.mcCartel.onPress=function(){
				SGSBridge.getActive().SWF_2_SGS_setActiveGame(SGSBridge.getActive()._games.getItem(String(target)));	
			};
		};

		for(var i:Number=0; i < games.getItems().length ; i++){
			var level:Number= GAMES_LEVEL + i;
			_games.addItem("_level"+String(level), games.getItems()[i]);
			loader.loadClip("menu_"+games.getItems()[i].getGamePath(), level);
		};
	}; //fin función

	public function SGS_2_SWF_gameLogOut():Void {
		for(var i:Number=GAMES_LEVEL; i < GAME_ACTIVE_LEVEL+1; i++){
			loader.unloadClip(i);
		};
	}; //fin función
	
	
	public function SGS_2_SWF_loadPlayer(player:Player):Void {
		var root:SGSBridge= this;
		parent.mcPlayer.gotoAndStop("logOut");
		parent.mcPlayer.txt_bet.text="";
		parent.mcPlayer.txt_saldo.text=player.getBalance().getCredit();
		trace("SGSBridge.as | player.getCredit()= "+parent.mcPlayer.txt_saldo.text);
		parent.mcPlayer.mcPlayerLogOut.onPress=function(){
			root.SWF_2_SGS_playerLogOut();
		};
	}; //fin función
	
	public function SGS_2_SWF_setPlayerCredits(betCredits:Number, credits:Number):Void {
		
		parent.mcPlayer.txt_bet.text=betCredits;
		parent.mcPlayer.txt_saldo.text=credits;

	}; //fin función
	
	public function SGS_2_SWF_activateGame(game:Game):Void {
		for(var i:Number=GAMES_LEVEL; i < GAMES_LEVEL_MAX; i++){
			loader.unloadClip(i);
		};
		
		//TODO hardcoding!!!!!
		game.setGamePath("slotZero.swf");
		delete loaderList.onLoadInit;
		loader.loadClip(game.getGamePath(), GAME_ACTIVE_LEVEL);
		
	}; //fin función
	
	
	////FUNCIONES DEL SWF AL SGS
	
	public function SWF_2_SGS_playerLogIn(serial:String):Void {
		SGS.getActive().playerLogIn(serial);
	}; //fin función

	public function SWF_2_SGS_playerLogOut(serial:String):Void {
		SGS.getActive().playerLogOut();
		loader.unloadClip(GAME_ACTIVE_LEVEL);
		parent.mcPlayer.gotoAndStop("logIn");
		init(this);
	}; //fin función

	public function SWF_2_SGS_setActiveGame(game:Game):Void {
		SGS.getActive().setActiveGame(game);
	}; //fin función
	
	////FUNCIONES PRIVADAS
	private function init(root:SGSBridge):Void{
		parent.mcPlayer.mcPlayerLogIn.onPress= function(){
			root.SWF_2_SGS_playerLogIn(this._parent.txt_playerSerial.text);
		};
	};
	
}; // fin de clase